package com.zoe.app;

import cn.hutool.core.util.StrUtil;
import com.zoe.app.annotation.DataFilter;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zoe.Zhang
 * @date 2022/05/26
 * @description
 */
@Aspect
@Component
public class DataAspect1 {

    @SneakyThrows
    @Around(value = "@annotation(dataFilter)")
    public Object aroundPoint(ProceedingJoinPoint proceedingJoinPoint, DataFilter dataFilter) {

        Object o = proceedingJoinPoint.proceed();

        if (dataFilter.enable()) {
            String filterValue = dataFilter.filterValue();
            if (o instanceof List) {
                List<?> o1 = (List<?>) o;
                o = o1.stream()
                      .filter(st -> !StrUtil.contains(st.toString(), filterValue))
                      .collect(Collectors.toList());
            }
        }
        return o;
    }

}
